package net.caixiaomi.info.Lottery.dialog;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.reflect.TypeToken;

import net.caixiaomi.info.Lottery.model.LotteryTrendParamsEntity;
import net.caixiaomi.info.R;
import net.caixiaomi.info.base.BaseDialogFragment;
import net.caixiaomi.info.base.quickadapter.BaseQuickAdapter;
import net.caixiaomi.info.model.CommonEvent;
import net.caixiaomi.info.model.MatchItem;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static android.R.id.button1;
import static android.R.id.button2;
import static net.caixiaomi.info.util.EventWhat.EVENT_FOOTBALL_MATCH_FILTER;
import static net.caixiaomi.info.util.EventWhat.EVENT_TREND;

/**
 * Created by suntian on 2018/8/17.
 */

public class TrendSettingDialog extends BaseDialogFragment implements BaseQuickAdapter.OnItemClickListener {

    private int compute=1;  //0不统计，1统计
    private int count=100;  //期数，30，50，100
    private int drop=1;   //是否显示遗漏0不显示，1显示
    private int sort=0;   //排序, 0正序1倒序

    @BindView(R.id.trend_setting_title)
    TextView mTitle;

    @BindView(R.id.trend_setting_30_img)
    ImageView trend_setting_30_img;

    @BindView(R.id.trend_setting_50_img)
    ImageView trend_setting_50_img;

    @BindView(R.id.trend_setting_100_img)
    ImageView trend_setting_100_img;

    @BindView(R.id.trend_setting_leave_on_img)
    ImageView trend_setting_leave_on_img;

    @BindView(R.id.trend_setting_leave_off_img)
    ImageView trend_setting_leave_off_img;

    @BindView(R.id.trend_setting_count_on_img)
    ImageView trend_setting_count_on_img;

    @BindView(R.id.trend_setting_count_off_img)
    ImageView trend_setting_count_off_img;

    @BindView(R.id.trend_setting_sort_on_img)
    ImageView trend_setting_sort_on_img;

    @BindView(R.id.trend_setting_sort_off_img)
    ImageView trend_setting_sort_off_img;

    @OnClick(R.id.trend_setting_30)
    void trend_setting_30() {
        count=30;
        trend_setting_30_img.setBackgroundResource(R.drawable.selected_box_img);
        trend_setting_50_img.setBackgroundResource(R.drawable.selection_box_img);
        trend_setting_100_img.setBackgroundResource(R.drawable.selection_box_img);
    }

    @OnClick(R.id.trend_setting_50)
    void trend_setting_50() {
        count=50;
        trend_setting_30_img.setBackgroundResource(R.drawable.selection_box_img);
        trend_setting_50_img.setBackgroundResource(R.drawable.selected_box_img);
        trend_setting_100_img.setBackgroundResource(R.drawable.selection_box_img);
    }

    @OnClick(R.id.trend_setting_100)
    void trend_setting_100() {
        count=100;
        trend_setting_30_img.setBackgroundResource(R.drawable.selection_box_img);
        trend_setting_50_img.setBackgroundResource(R.drawable.selection_box_img);
        trend_setting_100_img.setBackgroundResource(R.drawable.selected_box_img);
    }

    @OnClick(R.id.trend_setting_leave_on)
    void trend_setting_leave_on() {
        drop=1;
        trend_setting_leave_on_img.setBackgroundResource(R.drawable.selected_box_img);
        trend_setting_leave_off_img.setBackgroundResource(R.drawable.selection_box_img);
    }

    @OnClick(R.id.trend_setting_leave_off)
    void trend_setting_leave_off() {
        drop=0;
        trend_setting_leave_on_img.setBackgroundResource(R.drawable.selection_box_img);
        trend_setting_leave_off_img.setBackgroundResource(R.drawable.selected_box_img);
    }

    @OnClick(R.id.trend_setting_count_on)
    void trend_setting_count_on() {
        compute=1;
        trend_setting_count_on_img.setBackgroundResource(R.drawable.selected_box_img);
        trend_setting_count_off_img.setBackgroundResource(R.drawable.selection_box_img);
    }

    @OnClick(R.id.trend_setting_count_off)
    void trend_setting_count_off() {
        compute=0;
        trend_setting_count_on_img.setBackgroundResource(R.drawable.selection_box_img);
        trend_setting_count_off_img.setBackgroundResource(R.drawable.selected_box_img);
    }

    @OnClick(R.id.trend_setting_sort_on)
    void trend_setting_sort_on() {
        sort=0;
        trend_setting_sort_on_img.setBackgroundResource(R.drawable.selected_box_img);
        trend_setting_sort_off_img.setBackgroundResource(R.drawable.selection_box_img);
    }

    @OnClick(R.id.trend_setting_sort_off)
    void trend_setting_sort_off() {
        sort=1;
        trend_setting_sort_on_img.setBackgroundResource(R.drawable.selection_box_img);
        trend_setting_sort_off_img.setBackgroundResource(R.drawable.selected_box_img);
    }

    @OnClick(button1)
    void toConfirm() {
        LotteryTrendParamsEntity entity=new LotteryTrendParamsEntity();
        entity.setDrop(drop);
        entity.setSort(sort);
        entity.setCount(count);
        entity.setCompute(compute);
        EventBus.getDefault().post(new CommonEvent(EVENT_TREND, entity.toString()));
        dismissAllowingStateLoss();
    }

    @OnClick(button2)
    void toCancel() {
        dismiss();
    }

    @Override
    protected int layoutRes() {
        return R.layout.trend_setting_layout;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LotteryTrendParamsEntity entity= (LotteryTrendParamsEntity) getArguments().getSerializable("params");
        compute=entity.getCompute();
        count=entity.getCount();
        drop=entity.getDrop();
        sort=entity.getSort();
    }


    @Override
    protected void onViewCreated(@NonNull View view) {
        try {
            Window window = getDialog().getWindow();
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            window.setBackgroundDrawable(new ColorDrawable(0xff000000));
            getDialog().getWindow().setLayout((int) (dm.widthPixels * 0.85), getDialog().getWindow().getAttributes().height);
            getDialog().setCanceledOnTouchOutside(false);
            getDialog().setCancelable(false);
//            mTitle.setText(getString(R.string.app_name)+" · "+"走势图设置");
            mTitle.setText("走势图设置");
            initView();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initView(){
        switch (count){
            case 30:
                trend_setting_30_img.setBackgroundResource(R.drawable.selected_box_img);
                trend_setting_50_img.setBackgroundResource(R.drawable.selection_box_img);
                trend_setting_100_img.setBackgroundResource(R.drawable.selection_box_img);
                break;
            case 50:
                trend_setting_30_img.setBackgroundResource(R.drawable.selection_box_img);
                trend_setting_50_img.setBackgroundResource(R.drawable.selected_box_img);
                trend_setting_100_img.setBackgroundResource(R.drawable.selection_box_img);
                break;
            case 100:
                trend_setting_30_img.setBackgroundResource(R.drawable.selection_box_img);
                trend_setting_50_img.setBackgroundResource(R.drawable.selection_box_img);
                trend_setting_100_img.setBackgroundResource(R.drawable.selected_box_img);
                break;
        }
        if(compute==1){
            trend_setting_count_on_img.setBackgroundResource(R.drawable.selected_box_img);
            trend_setting_count_off_img.setBackgroundResource(R.drawable.selection_box_img);
        }else{
            trend_setting_count_on_img.setBackgroundResource(R.drawable.selection_box_img);
            trend_setting_count_off_img.setBackgroundResource(R.drawable.selected_box_img);
        }
        if(drop==1){
            trend_setting_leave_on_img.setBackgroundResource(R.drawable.selected_box_img);
            trend_setting_leave_off_img.setBackgroundResource(R.drawable.selection_box_img);
        }else{
            trend_setting_leave_on_img.setBackgroundResource(R.drawable.selection_box_img);
            trend_setting_leave_off_img.setBackgroundResource(R.drawable.selected_box_img);
        }
        if(sort==1){
            trend_setting_sort_on_img.setBackgroundResource(R.drawable.selection_box_img);
            trend_setting_sort_off_img.setBackgroundResource(R.drawable.selected_box_img);
        }else{
            trend_setting_sort_on_img.setBackgroundResource(R.drawable.selected_box_img);
            trend_setting_sort_off_img.setBackgroundResource(R.drawable.selection_box_img);
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }
}
